package controller;

import document.HtmlDocument;
import view.*;
import reader.ModelBuilder;

import javax.swing.*;
import java.awt.*;
import java.io.FileReader;

/**
 * Created by Pavel Nikolaev
 */
public class Controller {
    private ControlPanel myControlPanel;
    private HtmlPanel myHtmlPanel;
    private HighlightPanel myHighlightPanel;
    private JScrollPane myMainPanel;
    private SearchPanel mySearchPanel;

    private SelectionManager mySelectionManager;
    private DocumentHistory myHistory;

    public Controller() {
        myControlPanel = new ControlPanel();
        myHtmlPanel = new HtmlPanel();
        myHighlightPanel = new HighlightPanel(myHtmlPanel);
        myMainPanel = new HtmlScrollPanel(myHighlightPanel);
        mySearchPanel = new SearchPanel();

        mySelectionManager = new SelectionManager(myHighlightPanel);

        myHtmlPanel.addPropertyChangeListener(myControlPanel);
        myHtmlPanel.addPropertyChangeListener(mySearchPanel);
        myHtmlPanel.addPropertyChangeListener(myHighlightPanel);
        myHtmlPanel.addMouseListener(mySelectionManager);
        myHtmlPanel.addMouseMotionListener(mySelectionManager);

        myControlPanel.subscribe(this);
        mySearchPanel.subscribe(this);

        myHistory = new DocumentHistory();
    }

    public JFrame createFrame() {
        JFrame frame = new JFrame("Browser");
        Dimension dim = new Dimension(900, 500);
        frame.setSize(dim);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        frame.getContentPane().add(myControlPanel, BorderLayout.NORTH);
        frame.getContentPane().add(myMainPanel, BorderLayout.CENTER);
        frame.getContentPane().add(mySearchPanel, BorderLayout.SOUTH);

        return frame;
    }

    public boolean readFile(String file) {
        String dir = System.getProperty("user.dir");
        ModelBuilder builder = new ModelBuilder();
        try {
            FileReader in = new FileReader(dir + "\\" + file);
            builder.readDocument(in);
            in.close();
            HtmlDocument document = builder.getDocument();
            myHistory.add(document);
            myHtmlPanel.setDocument(document);
            myMainPanel.revalidate();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return myHistory.isBackwardStepAvailable();
    }

    public boolean stepBackInHistory() {
        boolean isPreviousAvailable = myHistory.goBack();
        myHtmlPanel.setDocument(myHistory.getDocument());
        return isPreviousAvailable;
    }

    public boolean stepForwardInHistory() {
        boolean isNextAvailable = myHistory.goForward();
        myHtmlPanel.setDocument(myHistory.getDocument());
        return isNextAvailable;
    }

    public void updateDocument() {
        myHtmlPanel.setDocument(myHistory.getDocument());
    }

    public void searchText(String text) {
        //TODO implement method
    }
}
